//
//  EveryDetailViewController.m
//  礼物
//
//  Created by dllo on 15/11/10.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "EveryDetailViewController.h"
#import "EveryDetailCellCollectionViewCell.h"
#import "EveryDetialModel.h"
#import "CategoryDetailModel.h"

@interface EveryDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate>


@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *everyDetailModelArray;

@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) UIView *introduceView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *priceLabel;
@property (nonatomic, retain) UILabel *descriptionLabel;

@property (nonatomic, assign) int n;//全局变量

@end

@implementation EveryDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.everyDetailModelArray = [NSMutableArray array];
    
    //默认n为0
    self.n = 0;
    
    //导航栏标题
    UILabel *titleLabels = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 4, 44)];
    titleLabels.text = @"商品详情";
    titleLabels.textColor = [UIColor whiteColor];
    titleLabels.textAlignment = NSTextAlignmentCenter;
    titleLabels.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
    self.navigationItem.titleView = titleLabels;
   
    
    
    //导航栏左侧按钮
    UIImage *image = [UIImage imageNamed:@"iconfont-jiantouicon-1"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT);
    //设置item的行间距
    flowLayout.minimumLineSpacing = 0;
    //设置item的列间距
    flowLayout.minimumInteritemSpacing = 0;
    //设置item的上,左,下,右 边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //头部区域大小
    //flowLayout.headerReferenceSize = CGSizeMake(WIDTH,  HEIGHT / 2 + HEIGHT / 3);
    //滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT ) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = RGBA(247, 247, 247, 1);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    //滚到边缘是否有反弹效果
    self.collectionView.bounces = NO;
    //是否显示横向滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    //是否显示竖向滚动条
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    //注册cell
    [self.collectionView registerClass:[EveryDetailCellCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    //注册表头和底部视图
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    [self getAFNetWorkingData:self.categoryDetail.id];
    
    

    // Do any additional setup after loading the view.
}
//请求数据
-(void)getAFNetWorkingData:(NSInteger)ids
{
    
    //接⼝
    NSString *urlString = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/items/%ld", ids];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dictionary = responseObject;
        
        NSMutableDictionary *dic = [dictionary objectForKey:@"data"];
        
        EveryDetialModel *everyDetialModel = [[EveryDetialModel alloc] init];
        [everyDetialModel setValuesForKeysWithDictionary:dic];
        everyDetialModel.descriptions = [dic objectForKey:@"description"];
        
        [self.everyDetailModelArray addObject:everyDetialModel];
        
        //刷新
        [self.collectionView reloadData];
        
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             //NSLog(@"error = %@",error);
         }];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.everyDetailModelArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    EveryDetailCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NSLog(@"???????????");
    EveryDetialModel *everyDetialModel = [self.everyDetailModelArray objectAtIndex:indexPath.row];
    cell.everyDetialModel = everyDetialModel;
    
    return cell;
    
}



- (void)leftBarAction
{
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
